package security;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;

import org.springframework.security.access.ConfigAttribute;
import org.springframework.security.access.SecurityConfig;
import org.springframework.security.web.FilterInvocation;
import org.springframework.security.web.access.intercept.FilterInvocationSecurityMetadataSource;
import org.springframework.security.web.util.AntUrlPathMatcher;
import org.springframework.security.web.util.UrlMatcher;

import service.interf.security.IResource;



public class MyInvocationSecurityMetadataSource implements
		FilterInvocationSecurityMetadataSource {
	private IResource iResource;
	private UrlMatcher urlMatcher = new AntUrlPathMatcher();;
	private static Map<String, Collection<ConfigAttribute>> resourceMap = null;

	
	public void setIResource(IResource iResource) {
		this.iResource = iResource;
	}
	public MyInvocationSecurityMetadataSource() {}
	public MyInvocationSecurityMetadataSource(IResource iResource) throws Exception {
		setIResource(iResource);
		loadResource();
	}
	/**
	 * 加载资源
	 * @throws Exception
	 */
	public void loadResource() throws Exception {
		if(resourceMap != null)
			return;

		resourceMap = new HashMap<String, Collection<ConfigAttribute>>();
		Map map = iResource.getAllResourcesWithAuth();
		if(null != map && !map.isEmpty()){
			Set ks = map.keySet();
			for(Iterator ite=map.entrySet().iterator();ite.hasNext();){
				Collection<ConfigAttribute> atts = new ArrayList<ConfigAttribute>();
				Entry en = (Entry)ite.next();
				ArrayList<String> al = (ArrayList)en.getValue();
				for(String au:al){
					ConfigAttribute ca = new SecurityConfig(au);
					atts.add(ca);
				}
				resourceMap.put((String)en.getKey(), atts);
			}
		}
	}
	/**
	 * 更改权限
	 */
	public synchronized static void changeAuthority(String url,ArrayList<String> v){
		Collection<ConfigAttribute> atts = new ArrayList<ConfigAttribute>();
		for(String au:v){
			ConfigAttribute ca = new SecurityConfig(au);
			atts.add(ca);
		}
		if(v.size() == 0)
			resourceMap.remove(url);
		else
			resourceMap.put(url, atts);
	}
	/**
	 * 获取权限 
	 * @param object:需要获取权限的资源
	 */
	public Collection<ConfigAttribute> getAttributes(Object object)
			throws IllegalArgumentException {
		String url = ((FilterInvocation) object).getRequestUrl();
		if(url.indexOf("?")>-1)
			url = url.substring(0, url.indexOf("?"));
		else if(url.indexOf("#")>-1)
			url = url.substring(0, url.indexOf("#"));
		Iterator<String> ite = resourceMap.keySet().iterator();
		while (ite.hasNext()) {
			String resURL = ite.next();
			if (urlMatcher.pathMatchesUrl(url, resURL)) {
				return resourceMap.get(resURL);
			}
		}
		return null;
	}

	public boolean supports(Class<?> clazz) {
		return true;
	}

	public Collection<ConfigAttribute> getAllConfigAttributes() {
		return null;
	}
}